package ua.kpi_java_training.morochenets.module2;

/**
 * Class where the calculated potential and current position of predators and prey
 *
 * @author Ari100tell
 * @data: 01.06.13
 * @time: 16:15
 */
public class Position {

    private int width = 0;
    private int length = 0;

    public Position(int width, int length) {
        this.width = width;
        this.length = length;

    }

    /**
     * @param positionLength Location of fish along the horizontal axis
     * @param positionWidth  Location fish ordinate
     * @param placesForLife  Array containing information about fish in the pool
     */

    public int moving(int positionLength, int positionWidth, int[][] placesForLife) {
        int direction = (int) (Math.random() * 4);
        System.out.println("direction=" + direction);
        switch (direction) {
            case 0: {
                return movingUp(positionLength, positionWidth, placesForLife);
            }
            case 1: {
                return movingDown(positionLength, positionWidth, placesForLife);
            }
            case 2: {
                return movingLeft(positionLength, positionWidth, placesForLife);
            }
            case 3: {
                return movingRight(positionLength, positionWidth, placesForLife);
            }
        }
        return 0;
    }

    private int movingDown(int positionLength, int positionWidth, int[][] placesForLife) {
        if ((positionLength != (length - 1)) && (positionWidth != (width - 1))) {
            if (placesForLife[positionLength][positionWidth + 1] != placesForLife[positionLength][positionWidth]) {
                placesForLife[positionLength][positionWidth + 1] = placesForLife[positionLength][positionWidth] * 2;
                placesForLife[positionLength][positionWidth] = 0;
                return 0;
            } else return repeatMove(positionLength, positionWidth, placesForLife);
        } else {
            return repeatMove(positionLength, positionWidth, placesForLife);
        }
    }

    private int repeatMove(int positionLength, int positionWidth, int[][] placesForLife) {
        moving(positionLength, positionWidth, placesForLife);
        return 0;
    }

    private int movingLeft(int positionLength, int positionWidth, int[][] placesForLife) {
        if (positionWidth != 0) {
            if (placesForLife[positionLength][positionWidth - 1] != placesForLife[positionLength][positionWidth]) {
                placesForLife[positionLength][positionWidth - 1] = placesForLife[positionLength][positionWidth] * 2;
                placesForLife[positionLength][positionWidth] = 0;
                return 0;
            } else {
                return repeatMove(positionLength, positionWidth, placesForLife);
            }
        } else {
            return repeatMove(positionLength, positionWidth, placesForLife);
        }
    }

    private int movingRight(int positionLength, int positionWidth, int[][] placesForLife) {
        if ((positionWidth != (width - 1)) && (positionLength != (length - 1))) {
            if (placesForLife[positionLength + 1][positionWidth] != placesForLife[positionLength][positionWidth]) {
                placesForLife[positionLength + 1][positionWidth] = placesForLife[positionLength][positionWidth] * 2;
                placesForLife[positionLength][positionWidth] = 0;
                return 0;
            } else {
                return repeatMove(positionLength, positionWidth, placesForLife);
            }
        } else {
            return repeatMove(positionLength, positionWidth, placesForLife);
        }
    }

    private int movingUp(int positionLength, int positionWidth, int[][] placesForLife) {
        if (positionLength != 0) {
            if (placesForLife[positionLength - 1][positionWidth] != placesForLife[positionLength][positionWidth]) {
                placesForLife[positionLength - 1][positionWidth] = placesForLife[positionLength][positionWidth] * 2;
                placesForLife[positionLength][positionWidth] = 0;
                return 0;
            } else {
                return repeatMove(positionLength, positionWidth, placesForLife);
            }
        } else {
            return repeatMove(positionLength, positionWidth, placesForLife);
        }
    }

    public int[][] changePosition(int[][] placesForLife) {
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < length; j++) {
                if ((placesForLife[i][j] != 0) && (Math.abs(placesForLife[i][j]) < 2)) {
                    moving(i, j, placesForLife);
                }
            }
        }
        placesForLife = deletePosition(placesForLife);
        return placesForLife;
    }

    public int[][] deletePosition(int[][] placesForLife) {
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < length; j++) {
                placesForLife[i][j] /= 2;
            }
        }
        return placesForLife;
    }
}